package com.hs.fm;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.chiemy.cardview.FMFragment;
import com.chiemy.cardview.R;
import com.chiemy.cardview.Utils;
import com.hs.color.lamp.BluetoothChatService;
import com.hs.color.lamp.SppCommandPacket;

import java.text.DecimalFormat;

public class FMCntrlFrangment extends Fragment {
	
	private String TAG = "FMCntrlFrangment";
	private int mCurrentStation = 950; //default 95.0 MHz
	private View root; 
	private Context context;
	public static BluetoothChatService mChatService = null;
	private ImageView mThoundSandDigit = null;
    private ImageView mHundredDigit = null;
    private ImageView mSingleDigit = null;
    private ImageView mFloatDigit = null;
    private TextView mTextMHz = null;
    
    
	private SeekBar mFMProgressbar = null;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.context = activity;
	}


	@SuppressWarnings("deprecation")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = View.inflate(getActivity(), R.layout.fm_ctl_frag, null);
		 
		mChatService = FMFragment.mChatService;
		
		mTextMHz = (TextView) root.findViewById(R.id.text_mhz);
        mTextMHz.setText("MHz");
		mFMProgressbar = (SeekBar) root.findViewById(R.id.progressbar);
		mThoundSandDigit = (ImageView) root.findViewById(R.id.value1);
        mHundredDigit = (ImageView) root.findViewById(R.id.value2);
        mSingleDigit = (ImageView) root.findViewById(R.id.value3);
        mFloatDigit = (ImageView) root.findViewById(R.id.value4);
		
		mFMProgressbar.setMax(205);
		mFMProgressbar.setProgress(mCurrentStation-875);
        mFMProgressbar.setOnSeekBarChangeListener(mSeekListener);
        
        DecimalFormat   fnum  =   new  DecimalFormat("##0.0");  
		String dd = fnum.format((float)mCurrentStation/10);
		setStationValue(dd);
		return root;
	}
	
	
	private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {

		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			
			Log.i(TAG, "<onProgressChanged> progress=" + progress);
			int pro = progress;
			DecimalFormat   fnum  =   new  DecimalFormat("##0.0");  
			String dd = fnum.format((float)(pro + 875)/10);
			setStationValue(dd);
			//setStationValue(String.valueOf((float)(pro + 875)/10));
			
		}

		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}

		public void onStopTrackingTouch(SeekBar seekBar) {
			int pro = mFMProgressbar.getProgress();
			long CurrentStation = pro + 875;
			
			DecimalFormat   fnum  =   new  DecimalFormat("##0.0");  
			String dd = fnum.format((float)(pro + 875)/10);
			setStationValue(dd);
		    Log.i(TAG,"<onStopTrackingTouch> pro="+pro+", mCurrentStation = "+mCurrentStation+", String.valueOf((float)(pro+875)/10)="+String.valueOf((float)(pro+875)/10));
			
		    //TODO 
		    byte[] mCmdPacket = null;
			byte[] fm_array = new byte[2];
			Utils.uint16_to_bytearray(CurrentStation*10, fm_array);
			mCmdPacket = SppCommandPacket.getSendFmFrqCtrlPacket(fm_array);
		    if(mChatService != null)
			{
				if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
				    Toast.makeText(context, "spp not connected", Toast.LENGTH_SHORT).show();
				    return;
				}
				mChatService.write(mCmdPacket);
				Toast.makeText(context, "spp send command", Toast.LENGTH_SHORT).show();
			}
		}
    };
    
    
    
    
    
    
    
	private void setStationValue(String stationWithoutFloat) {
    	Log.i("FMCntrlFrangment","<setStationValue> stationWithoutFloat = "+stationWithoutFloat);
		int thousandDigit = 0;
		int hundredDigit = 0;
		int singleDigit = 0;
		int floatDigit = 0;

		if (stationWithoutFloat.length() == 5) {
			thousandDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(0)));
			hundredDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(1)));
			singleDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(2)));
			floatDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(4)));
		} else if (stationWithoutFloat.length() == 4) {
			thousandDigit = -1;
			hundredDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(0)));
			singleDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(1)));
			floatDigit = Integer.parseInt(String.valueOf(stationWithoutFloat.charAt(3)));
		}
		 
		if (thousandDigit == -1) {
			mThoundSandDigit.setBackgroundResource(R.drawable.figure_zero);
			mThoundSandDigit.setVisibility(View.INVISIBLE);
		} else {
            mThoundSandDigit.setVisibility(View.VISIBLE);
			mThoundSandDigit.setBackgroundResource(getDigitResource(thousandDigit));
		}
	
        mHundredDigit.setBackgroundResource(getDigitResource(hundredDigit));
        mSingleDigit.setBackgroundResource(getDigitResource(singleDigit));
        mFloatDigit.setBackgroundResource(getDigitResource(floatDigit));
    }

    private int getDigitResource(int num) {
    	switch (num) {
	    	case 0:
	    		return R.drawable.figure_zero;
	    	case 1:
	    		return R.drawable.figure_one;
	    	case 2:
	    		return R.drawable.figure_two;
	    	case 3:
	    		return R.drawable.figure_three;
	    	case 4:
	    		return R.drawable.figure_four;
	    	case 5:
	    		return R.drawable.figure_five;
	    	case 6:
	    		return R.drawable.figure_six;
	    	case 7:
	    		return R.drawable.figure_seven;
	    	case 8:
	    		return R.drawable.figure_eight;
	    	case 9:
	    		return R.drawable.figure_nine;
	    	default:
	    		return R.drawable.figure_one;
    	}
    }
    
    
    
}
